package com.neusoft.lohas.modules.ivm.web.mall;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.utils.IdGen;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.mall.Index;
import com.neusoft.lohas.modules.ivm.entity.mall.IndexHot;
import com.neusoft.lohas.modules.ivm.entity.mall.IndexPhoto;
import com.neusoft.lohas.modules.ivm.service.mall.IndexPhotoService;
/**
 * 商圈最新优惠Controller
 * @author Jackie.Liu
 * @version 2015-01-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/mall")
public class IndexController extends BaseController {

	@Autowired
	private IndexPhotoService indexPhotoService;
	
	@RequiresPermissions("ivm:mall:index:view")
	@RequestMapping(value = "index")
	public String form(Model model) {
		List<IndexPhoto> photoList = indexPhotoService.findList();
		model.addAttribute("photoList", photoList);
		return "modules/ivm/mall/indexForm";
	}

	@RequiresPermissions("ivm:mall:index:edit")
	@RequestMapping(value = "index/save")
	public String save(Index index, Model model, RedirectAttributes redirectAttributes) {
		for (IndexPhoto photo : index.getPhotoList()) {
			if (StringUtils.isBlank(photo.getId())) {
				photo.setId(IdGen.uuid());
				photo.prePersist();
			}
			for (IndexHot hot : photo.getHotList()) {
				if (StringUtils.isBlank(hot.getId())) {
					hot.setIndexPhoto(photo);
					hot.setId(IdGen.uuid());
					hot.prePersist();
				}
			}
		}
		indexPhotoService.save(index.getPhotoList());
		addMessage(redirectAttributes, "保存首页设置成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/mall/index/?repage";
	}
}
